﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FineUICore;
using Microsoft.AspNetCore.Mvc;
using MyAppBox.Controllers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace MyAppBox.Areas.system.Controllers
{
    [Area("system")]
    public class system10Controller : SysPageController
    {
        // GET: system/system10
        public ActionResult Index()
        {
            InitData();
            return View();
        }
        #region 初始化数据

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            MsQuery ms = new MsQuery();
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 ";
                //只能看见自己和自己新增的角色
            }
            DataTable dt = ((DataSet)new BLLsystem_10().Getsystem_10ByQuery(ms).data).Tables[0];
            string sys10 = dt.Rows[0]["System_10_10"].ToString();
            ViewBag.griddata = dt;
            //ViewBag.Grid1data = GetGrid1Data(sys10);
        }

        private DataTable GetGrid1Data(string str10)
        {
            MsQuery ms = new MsQuery();
            DataTable dt = new DataTable();
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 and SYSTEM_R3_30 ='" + str10 + "' and SYSTEM_R3_40='1'";
            }
            //ms.strorder = "  ";//排序语句
            try
            {
                List<Mssystem_r3> listms = new BLLsystem_r3().Getsystem_r3MsByQuery(ms);

                ms = new MsQuery();
                //条件
                if (true)
                {
                    ms.strquery += " and 1=1 and SYSTEM_1_10 in (" + string.Join(",", listms.ConvertAll(m => m.System_R3_20)) + ") ";
                    //ms.strorder = " SYSTEM_7_100 ";
                }
                dt = ((DataSet)new BLLsystem_1().Getsystem_1ByQuery(ms).data).Tables[0];
            }
            catch { }
            return dt;
        }



        /// <summary>
        /// 得到按钮组
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, object> GetSys7Listdata(DataTable dt, string sys10)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            StringWriter swshow = new StringWriter(sb);
            JsonTextWriter wr = new JsonTextWriter(swshow);
            List<string> srows = new List<string>();
            wr.Formatting = Formatting.Indented;
            wr.WriteStartArray();
            foreach (DataRow row in dt.Rows)
            {
                wr.WriteStartObject();
                MsQuery msq = new MsQuery();
                msq.strquery += " and SYSTEM_8_40 ='" + row["SYSTEM_7_30"].ToString() + "'";
                msq.strquery += " and 1=1 and (SYSTEM_8_50='1' or SYSTEM_8_50 is null) ";
                msq.strorder = " SYSTEM_8_60 ";//排序语句
                List<Mssystem_8> listms = new BLLsystem_8().Getsystem_8MsByQuery(msq);
                List<Mssystem_13> listms13 = new List<Mssystem_13>();
                MsQuery msq13 = new MsQuery();
                msq13.strquery += " and  System_13_20 = '" + sys10 + "' ";
                msq13.strquery += " and  System_13_30 = '" + row["SYSTEM_7_10"].ToString() + "' ";
                msq13.strquery += " and  SYSTEM_13_70 = '1' ";
                listms13 = new BLLsystem_13().Getsystem_13MsByQuery(msq13);
                wr.WritePropertyName("str10");
                wr.WriteValue(row["SYSTEM_7_10"].ToString());
                wr.WritePropertyName("data");
                wr.WriteStartArray();
                foreach (var item in listms)
                {
                    wr.WriteStartArray();
                    wr.WriteValue(item.System_8_10.ToString());
                    wr.WriteValue(item.System_8_20);
                    wr.WriteEndArray();
                }
                wr.WriteEndArray();
                wr.WritePropertyName("value");
                wr.WriteStartArray();
                foreach (var item in listms13)
                {
                    wr.WriteValue(item.System_13_50);
                }
                wr.WriteEndArray();
                wr.WriteEndObject();

                try
                {
                    List<Mssystem_12> listms12 = new List<Mssystem_12>();
                    MsQuery msq12 = new MsQuery();
                    msq12.strquery += " and  System_12_20 = '" + sys10 + "' ";
                    msq12.strquery += " and  SYSTEM_12_40 = '1' ";
                    listms12 = new BLLsystem_12().Getsystem_12MsByQuery(msq12);
                    //判断菜单勾选
                    if (listms12.ConvertAll(m => m.System_12_30).Contains(Convert.ToInt32(row["SYSTEM_7_10"].ToString())))
                    {
                        srows.Add(row["SYSTEM_7_10"].ToString());
                    }
                }
                catch { }
            }
            wr.WriteEndArray();
            dic["sys7selectdata"] = string.Join(",", srows);
            dic["sys7listdata"] = sb.ToString().Replace("\r\n", "");
            return dic;
        }
        #endregion

        /// <summary>
        /// 选择按钮事件
        /// </summary>
        /// <param name="str10"></param>
        /// <param name="sys8list"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys7_selectchange(string sys10, string str10, JArray sys8list)
        {
            //把13下该角色改菜单按钮都删除，在信息
            MsQuery msq = new MsQuery();
            msq.strquery = " and System_13_20='" + sys10 + "'";
            msq.strquery += " and System_13_30='" + str10 + "'";
            new BLLsystem_13().Deletesystem_13ByQuery(msq);
            Mssystem_13 ms = new Mssystem_13();
            ms.System_13_20 = sys10;
            ms.System_13_30 = str10;
            foreach (var item in sys8list)
            {
                ms.System_13_50 = item.ToString();
                ms.System_13_70 = "1";
                new BLLsystem_13().Savesystem_13(ms);
            }
            ShowNotify("保存成功");
            return FineUICore.UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult bendeluser(string str10, JArray uid, JArray Grid1field)
        {
            MsQuery msq = new MsQuery();
            msq.strquery += " and System_R3_30 = " + str10 + " and System_R3_20 in ('" + string.Join(",", uid) + "')";
            new BLLsystem_r3().Deletesystem_r3ByQuery(msq);
            DataTable dt = GetGrid1Data(str10);
            var Grid1 = UIHelper.Grid("Grid1");
            Grid1.DataSource(dt, Grid1field);
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 选择角色
        /// </summary>
        /// <param name="str10">角色ID</param>
        /// <param name="Sys7Gridfields"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys10_selectchange(string str10, JArray Sys7Gridfields, JArray Grid1fields)
        {
            var Sys7Grid = UIHelper.Grid("Sys7Grid");
            var Grid1 = UIHelper.Grid("Grid1");
            MsQuery ms = new MsQuery();
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 and SYSTEM_7_40='1' ";
                ms.strorder = " SYSTEM_7_100 ";
                Mssystem_1 mssys1 = GetSession<Mssystem_1>("LoginUser");
                if (mssys1 != null && !mssys1.isadmin)
                {
                    ms.strquery += " and SYSTEM_7_10 in ('" + string.Join("','", mssys1.menuids) + "') ";
                }
            }
            DataTable dt = ((DataSet)new BLLsystem_7().Getsystem_7ByQuery(ms).data).Tables[0];
            dt = FineUIEx.GetNewTreeDt(dt, "SYSTEM_7_10", "SYSTEM_7_60");
            Sys7Grid.DataSource(dt, Sys7Gridfields);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic = GetSys7Listdata(dt, str10);
            //Sys7Grid.Attribute("sys7selectdata", dic["sys7selectdata"].ToString());
            //Sys7Grid.Attribute("sys7listdata", dic["sys7listdata"].ToString().Replace("\"","'"));
            StringBuilder sb = new StringBuilder();
            sb.Append("F.ui.Sys7Grid.sys7selectdata=" + (dic["sys7selectdata"].ToString() == "" ? "[]" : dic["sys7selectdata"].ToString()) + ";");
            sb.Append("F.ui.Sys7Grid.sys7listdata=" + dic["sys7listdata"].ToString() + ";");
            FineUICore.PageContext.RegisterStartupScript(sb.ToString());
            //if (dic["sys7selectdata"].ToString()!="")
            {
                Sys7Grid.SelectedRowIDArray(dic["sys7selectdata"].ToString().Split(','));
            }
            //else { Sys7Grid.SelectedRowIDArray("-1"); }
            Grid1.DataSource(GetGrid1Data(str10), Grid1fields);
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 选择人员确认
        /// </summary>
        /// <param name="str10"></param>
        /// <param name="uid"></param>
        /// <param name="Grid1field"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult selectuser(string str10, JArray uid, JArray Grid1field)
        {
            Mssystem_r3 r3 = new Mssystem_r3();
            r3.System_R3_30 = str10;
            r3.System_R3_40 = "1";
            r3.System_R3_50 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            try
            {
                foreach (string item in uid)
                {
                    r3.System_R3_20 = item;
                    new BLLsystem_r3().Savesystem_r3(r3);
                }
            }
            catch { }
            DataTable dt = GetGrid1Data(str10);
            var Grid1 = UIHelper.Grid("Grid1");
            Grid1.DataSource(dt, Grid1field);
            return FineUICore.UIHelper.Result();
        }


        /// <summary>
        /// 选择菜单事件
        /// </summary>
        /// <param name="sys10">角色ID</param>
        /// <param name="str10">菜单</param>
        /// <param name="check">是否选中0未选中</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Sys7Grid_selectchange(string sys10, string str10, string check)
        {
            if (check == "0")
            {
                MsQuery msq12 = new MsQuery();
                msq12.strquery += " and SYSTEM_12_20='" + sys10 + "'";
                msq12.strquery += " and SYSTEM_12_30='" + Convert.ToInt32(str10) + "'";
                new BLLsystem_12().Deletesystem_12ByQuery(msq12);
            }
            else
            {
                Mssystem_12 ms12 = new Mssystem_12();
                ms12.System_12_20 = sys10;
                ms12.System_12_30 = Convert.ToInt32(str10);
                ms12.System_12_40 = "1";
                new BLLsystem_12().Savesystem_12(ms12);
            }
            //FineUIEx.ShowNotify("保存成功");
            return FineUICore.UIHelper.Result();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult btnsys10save_Click(string str10, string str20, JArray sys10field)
        {
            var sys10List = UIHelper.Grid("sys10List");
            Mssystem_10 ms = new Mssystem_10();
            ms.System_10_10 = Convert.ToInt32(str10);
            ms.System_10_60 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            ms.System_10_20 = str20;
            MsReturned msr = new BLLsystem_10().Savesystem_10(ms);
            if (Convert.ToInt32(msr.strMsId) > 0)
            {
                if (str10 != "0")
                {
                    FineUICore.PageContext.RegisterStartupScript("F.ui.sys10List.updateCellValue('" + str10 + "', 'System_10_20', '" + str20 + "');");
                }
                else
                {
                    MsQuery msq = new MsQuery();
                    //条件
                    if (true)
                    {
                        msq.strquery += " and 1=1 ";
                    }
                    DataTable dt = ((DataSet)new BLLsystem_10().Getsystem_10ByQuery(msq).data).Tables[0];
                    sys10List.DataSource(dt, sys10field);
                    sys10List.SelectedRowIDArray(msr.strMsId);
                }
            }
            ShowNotify(msr.strMS);
            return FineUICore.UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys10Listdelete_Click(string str10)
        {

            return FineUICore.UIHelper.Result();
        }
    }
}